$(function () {
    // 加载铸造车间列表
    vm.loadFoundryWorkshopList();
});

var vm = new Vue({
    el: '#main_app',
    data: {
        workshopList: [] , // 车间列表，指的是铸造车间列表
        workshopId: '0101', // 铸造车间ID,默认为铸造二车间的ID
        moldStatus: -1 ,
        moldStatusDataList: [], // 当前车间下的模具状态数据列表
        // 模具状态更新参数
        moldStatusUpdateParam:{
            moldId: '',
            moldStatus: 1
        },
        styleObject: {
            color: 'red',
            fontSize: '13px'
        },
        moldStatusCssList:[
            {
                status: 1,
                desc:'量产',
                isActived: false,
                disabled: false,
                css: {
                    'background-color': '#00b050',
                    color: 'black'
                },
                cssBak:{
                    'background-color': '#00b050',
                    color: 'black'
                }
            },
            {
                status: 2,
                desc:'超寿命使用',
                isActived: false,
                disabled: false,
                css: {
                    'background-color': '#fabf8f',
                    color: 'black'
                },
                cssBak:{
                    'background-color': '#fabf8f',
                    color: 'black'
                }
            },
            {
                status: 3,
                desc:'试模',
                isActived: false,
                disabled: true,
                css: {
                    'background-color': '#ffff00',
                    color: 'black'
                },
                cssBak:{
                    'background-color': '#ffff00',
                    color: 'black'
                }
            },
            {
                status: 4,
                desc:'维护',
                isActived: false,
                disabled: false,
                css: {
                    'background-color': '#ffc000',
                    color: 'black'
                },
                cssBak:{
                    'background-color': '#ffc000',
                    color: 'black'
                }
            },
            {
                status: 5,
                desc:'维修',
                isActived: false,
                disabled: true,
                css: {
                    'background-color': '#ffc000',
                    color: 'black'
                },
                cssBak:{
                    'background-color': '#ffc000',
                    color: 'black'
                }
            },
            {
                status: 6,
                desc:'封存',
                isActived: false,
                disabled: false,
                css: {
                    'background-color': '#52c9e6',
                    color: 'black'
                },
                cssBak:{
                    'background-color': '#52c9e6',
                    color: 'black'
                }
            },
            {
                status: 7,
                desc:'外借',
                isActived: false,
                disabled: false,
                css: {
                    'background-color': '#7030a0',
                    color: 'black'
                },
                cssBak:{
                    'background-color': '#7030a0',
                    color: 'black'
                }
            },
            // {
            //     status: 8,
            //     desc:'模具报废',
            //     isActived: false,
            //     disabled: false,
            //     css: {
            //         'background-color': '#ff0000',
            //         color: 'black'
            //     },
            //     cssBak:{
            //         'background-color': '#ff0000',
            //         color: 'black'
            //     }
            // },
            // {
            //     status: 9,
            //     desc:'镶块报废',
            //     isActived: false,
            //     disabled: false,
            //     css: {
            //         'background-color': '#ff0000',
            //         color: 'black'
            //     },
            //     cssBak:{
            //         'background-color': '#ff0000',
            //         color: 'black'
            //     }
            // },
            {
                status: 10,
                desc:'配新镶块',
                isActived: false,
                disabled: false,
                css: {
                    'background-color': '#bfbfbf',
                    color: 'black'
                },
                cssBak:{
                    'background-color': '#bfbfbf',
                    color: 'black'
                }
            }
        ]
    },
    methods: {
        loadMoldByStatus(moldStatusCss){
            // 除了点击元素之外的部分都得恢复正常样式
            for (let i = 0; i < this.moldStatusCssList.length; i++) {
                if(this.moldStatusCssList[i].status != moldStatusCss.status){
                    this.moldStatusCssList[i].css =  this.moldStatusCssList[i].cssBak;
                    this.moldStatusCssList[i].isActived = false;
                }
            }
            moldStatusCss.isActived = !moldStatusCss.isActived;
            if(!moldStatusCss.isActived){
                this.moldStatus = -1;
                moldStatusCss.css = moldStatusCss.cssBak;
            }else{
                this.moldStatus = moldStatusCss.status;
                moldStatusCss.css = {
                    'background-color': '#fcfbfa',
                    color: 'black'
                };
            }
            this.loadMoldStatusDataList();
        },
        renderItem(moldType){
            for (var i = 0; i < this.moldStatusCssList.length; i++) {
                if(this.moldStatusCssList[i].status == moldType){
                    return this.moldStatusCssList[i].cssBak;
                }
            }
            return this.moldStatusCssList[0].cssBak;
        },
        // 加载铸造车间列表
        loadFoundryWorkshopList: function () {
            let _this = this;
            Ajax.request({
                url: '../sys/dept/loadFoundryWorkshopList',
                async: false,
                successCallback: function (r) {
                    _this.workshopList = r.foundryWorkshopList;
                }
            });
        },
        query(){
            this.moldStatus = -1;
            this.loadMoldStatusDataList();
        },
        // 查看指定铸造车间下的模具健康地图
        loadMoldStatusDataList() {
            let _this = this;
            var requestUrl = "../modulearchives/loadMoldStatusData?foundryWorkshopId=" + this.workshopId;
            if(this.moldStatus != -1){
                requestUrl += '&moldStatus=' + this.moldStatus;
            }
            // 加载设备状态概览
            Ajax.request({
                url: requestUrl,
                contentType: "application/json",
                successCallback: function (res) {
                    if (res.code == 0) {
                        _this.moldStatusDataList = res.moldStatusDataList;
                        if(_this.moldStatusDataList.length == 0){
                            iview.Message.error('当前车间和状态下无可显示模具！');
                        }
                    }
                }
            });
        },
        // 更新模具状态
        updateMoldStatus(moldCurrentStatus) {
            this.moldStatusUpdateParam.moldStatus = moldCurrentStatus;
            let _this = this;
            // 加载设备状态概览
            Ajax.request({
                url: "../modulearchives/updateMoldStatus",
                params: JSON.stringify(this.moldStatusUpdateParam),
                type: "POST",
                contentType: "application/json",
                successCallback: function (res) {
                    if (res.code == 0) {
                        iview.Message.success('更新模具状态成功！');
                        _this.loadMoldStatusDataList();
                    } else {
                        iview.Message.error(r.msg);
                    }
                }
            });
        }
    },
    mounted: function () {
        this.loadMoldStatusDataList();
    }
});
